import { createRouterMap, createRouter } from "./createRouterMap.js";

export function createMatch(routes) {
  // 匹配器
  // 变成一个路由映射表
  const { pathMap } = createRouterMap(routes);
  // console.log(pathMap, "pathMap");
  // 动态添加路由 addRoutes
  // addRoutes(routes)
  function addRoutes(routes) {
    // 1. 用户自己的动态路由 保持之前的规则
    // 2. 追加的也需要和之前的进行合并

    createRouterMap(routes, pathMap);
  }

  // 案列 /about/c
  addRoutes([
    {
      path: "/cbout",
      component: "xx",
      children: [
        {
          path: "c",
          component: "ccc",
        },
      ],
    },
  ]);

  // console.log(pathMap, "pathMap");

  // 3. 路径要返回一个数组
  function match(location) {
    // 参数路由
    let recode = pathMap[location];
    // console.log("这里是一个数组", pathMap);
    if (recode) {
      return createRouter(recode, { path: location });
    }

    return createRouter(null, { path: location });
  }
  // console.log(match("/about"));
  return {
    addRoutes,
    match,
  };
}
